package com.cloud.model.entity;

public class ExpressionParser {
    // 输入的表达式
    private final String expression;
    // 当前解析的索引
    private int index;

    public ExpressionParser(String expression) {
        this.expression = expression;
        this.index = 0;
    }

    // 主解析方法
    public ASTNode parse() {
        return parseExpression();
    }

    // 解析 expression -> term (OR term)*
    private ASTNode parseExpression() {
        ASTNode node = parseTerm();
        while (match("or")) {
            ASTNode right = parseTerm();
            node = new OperatorNode("OR", node, right);
        }
        return node;
    }

    // 解析 term -> factor (AND factor)*
    private ASTNode parseTerm() {
        ASTNode node = parseFactor();
        while (match("and")) {
            ASTNode right = parseFactor();
            node = new OperatorNode("AND", node, right);
        }
        return node;
    }

    // 解析 factor -> condition | '(' expression ')'
    private ASTNode parseFactor() {
        if (match("(")) {
            ASTNode node = parseExpression();
            expect(")"); // 匹配括号
            return node;
        } else {
            return parseCondition();
        }
    }

    // 解析 condition -> <field> <operator> <value>
    private ASTNode parseCondition() {
        String field = parseField();       // 例如 "param1"
        String operator = parseOperator(); // 例如 "=="
        String value = parseValue();       // 例如 "'02-滚筒洗衣机'"
        return new ConditionNode(field, operator, value);
    }

    // 工具方法：匹配字段
    private String parseField() {
        skipWhitespace();
        int start = index;
        while (index < expression.length() && isValidFieldChar(expression.charAt(index))) {
            index++;
        }
        return expression.substring(start, index);
    }

    // 工具方法：匹配操作符
    private String parseOperator() {
        skipWhitespace();
        if (expression.startsWith("==", index)) {
            index += 2;
            return "==";
        }
        throw new RuntimeException("Invalid operator at index " + index);
    }

    // 工具方法：匹配值
    private String parseValue() {
        skipWhitespace();
        if (expression.charAt(index) == '\'') {
            int start = index;
            index++; // 跳过单引号
            while (index < expression.length() && expression.charAt(index) != '\'') {
                index++;
            }
            if (index >= expression.length() || expression.charAt(index) != '\'') {
                throw new RuntimeException("Unterminated string at index " + start);
            }
            index++; // 跳过末尾单引号
            return expression.substring(start, index);
        }
        throw new RuntimeException("Invalid value at index " + index);
    }

    // 工具方法：匹配特定符号
    private boolean match(String token) {
        skipWhitespace();
        if (expression.startsWith(token, index)) {
            index += token.length();
            skipWhitespace();
            return true;
        }
        return false;
    }

    // 工具方法：期待匹配
    private void expect(String token) {
        if (!match(token)) {
            throw new RuntimeException("Expected '" + token + "' at index " + index);
        }
    }

    // 工具方法：跳过空格
    private void skipWhitespace() {
        while (index < expression.length() && Character.isWhitespace(expression.charAt(index))) {
            index++;
        }
    }

    // 工具方法：合法字段字符
    private boolean isValidFieldChar(char c) {
        return Character.isLetterOrDigit(c) || c == '_';
    }
}